//
//  Login.m
//  视图控制器
//
//  Created by 18_李明明 on 14-11-15.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "Login.h"
#import "LabelAndTextField.h"
#import "Find.h"
#import "Registe.h"
#import "Movie.h"
#import "Rootcontain.h"
@interface Login ()<pageExchange>

@end

@implementation Login

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationController.navigationBar.translucent = NO;
    // 创建用户名
    self.title = @"登陆界面";
    LabelAndTextField *count = [[LabelAndTextField alloc] initWithFrame:CGRectMake(30, 70, 300, 40)];
    count.myLabel.text = @"用户名";
    count.myTextField.placeholder = @"手机/邮箱";
    [self.view addSubview:count];
    [count release];
    count.tag = 100;
    count.myTextField.delegate = self;
    count.myTextField.adjustsFontSizeToFitWidth = YES;
 
    // 创建密码
    LabelAndTextField *cipher = [[LabelAndTextField alloc] initWithFrame:CGRectMake(30, 130, 300, 40)];
    cipher.myLabel.text = @"密码";
    cipher.myTextField.placeholder = @"请输入密码";
    [self.view addSubview:cipher];
    cipher.myTextField.secureTextEntry = YES;
    [cipher release];
    cipher.myTextField.delegate = self;
    cipher.tag = 200;
    
    // 登陆按钮
    UIButton *login = [UIButton buttonWithType:UIButtonTypeSystem];
    [login setTitle:@"登陆" forState:UIControlStateNormal];
    login.frame = CGRectMake(40, 190, 80, 40);
    [self.view addSubview:login];
    [login addTarget:self action:@selector(targetLogin:) forControlEvents:UIControlEventTouchUpInside];
    login.backgroundColor = [UIColor greenColor];
    [login setTintColor:[UIColor whiteColor]];
    
    // 找回密码按钮
    UIButton *find = [UIButton buttonWithType:UIButtonTypeSystem];
    [find setTitle:@"找回密码" forState:UIControlStateNormal];
    find.frame = CGRectMake(140, 190, 80, 40);
    [self.view addSubview:find];
    [find addTarget:self action:@selector(targetF:) forControlEvents:UIControlEventTouchUpInside];
    find.backgroundColor = [UIColor purpleColor];
    [find setTintColor:[UIColor whiteColor]];
    
  
    
    
    
    // 注册按钮
    UIButton *registe = [UIButton buttonWithType:UIButtonTypeSystem];
    [registe setTitle:@"注册" forState:UIControlStateNormal];
    registe.frame = CGRectMake(240, 190, 80, 40);
    [self.view addSubview:registe];
    [registe addTarget:self action:@selector(targetR:) forControlEvents:UIControlEventTouchUpInside];
    registe.backgroundColor = [UIColor orangeColor];
    [registe setTintColor:[UIColor whiteColor]];
}




// 登陆按钮事件


- (void)targetLogin:(UIButton *)login
{
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    LabelAndTextField *count = (LabelAndTextField *)[self.view viewWithTag:100];
    [count.myTextField resignFirstResponder];
    LabelAndTextField *cipher = (LabelAndTextField *)[self.view viewWithTag:200];
    [cipher.myTextField resignFirstResponder];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@"1" forKey:@"l"];
    [userDefaults synchronize];
    if ((count.myTextField.text.length != 0) && (cipher.myTextField.text.length != 0)) {
        if ([[userDefaults objectForKey:@"l"] isEqualToString:@"1"]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登陆成功" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"OK", nil];
            [alert show];
            [alert release];
    // 跳向电影介绍界面
            Movie *movie = [[Movie alloc] init];
            [self.navigationController pushViewController:movie animated:YES];
            [movie release];
            
//            Movie *movie = [[Movie alloc] init];
//            [self.view removeFromSuperview];
//            [self.parentViewController addChildViewController:movie];
//            [self.parentViewController.view addSubview:movie.view];
//            [movie release];
            
        }else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"账号或密码输入有误" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"OK", nil];
            [alert show];
            [alert release];
        }
    }else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入内容不能为空" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
}

// 找回密码按钮事件
- (void)targetF:(UIButton *)login
{
    Find *find = [[Find alloc] init];
    [self.navigationController pushViewController:find animated:YES];
    [find release];
}
- (void)targetFind:(UIButton *)findbutton
{

//    Find *findWindow = [[Find alloc] init];
////    findWindow.modalTransitionStyle = 2;
////    [self presentViewController:findWindow animated:YES completion:^{
////    }];
//    [self.view addSubview:findWindow.view];
//    [self addChildViewController:findWindow];
//    [findWindow release];
    Find *find = [[Find alloc] init];
    [self.view removeFromSuperview];
    [self.parentViewController addChildViewController:find];
    [self.parentViewController.view addSubview:find.view];
    [find release];
}

// 注册按钮事件
- (void)targetR:(UIButton *)registe
{
    Registe *reg = [[Registe alloc] init];
    [self.navigationController pushViewController:reg animated:YES];
    [reg release];
}
- (void)targetRegiste:(UIButton *)reg
{
//    Registe *reg = [[Registe alloc] init];
////    reg.modalTransitionStyle = 2;
////    [self presentViewController:reg animated:YES completion:^{
////    }];
//    [self.view addSubview:reg.view];
//    [self addChildViewController:reg];
//    [reg release];
    
//    Rootcontain *root = [[Rootcontain alloc] init];
    Registe *registe = [[Registe alloc] init];
    [self.view removeFromSuperview];
    [self.parentViewController addChildViewController:registe];
    [self.parentViewController.view addSubview:registe.view];
    [registe release];
}
// 点击空白处回收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    LabelAndTextField *count = (LabelAndTextField *)[self.view viewWithTag:100];
    [count.myTextField resignFirstResponder];
    LabelAndTextField *cipher = (LabelAndTextField *)[self.view viewWithTag:200];
    [cipher.myTextField resignFirstResponder];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
